//
//  Date+LNG.swift
//  HanYuanSchool
//
//  Created by sun on 2022/7/26.
//  Copyright © 2022 hanyuan. All rights reserved.
//

import Foundation

public extension Date {
    /// 获取当前 秒级 时间戳 - 10位
    var timeStamp : Int {
        let timeInterval: TimeInterval = self.timeIntervalSince1970
        let timeStamp = Int(timeInterval)
        return timeStamp
    }
    /// 获取当前 秒级 时间戳字符串 - 10位
    var timeStampString : String {
        let timeInterval: TimeInterval = self.timeIntervalSince1970
        let timeStamp = Int(timeInterval)
        return "\(timeStamp)"
    }
    /// 获取当前 毫秒级 时间戳 - 13位
    var milliStamp : CLongLong {
        let timeInterval: TimeInterval = self.timeIntervalSince1970
        let millisecond = CLongLong(round(timeInterval*1000))
        return millisecond
    }
    /// 获取当前 毫秒级 时间戳字符串 - 13位
    var milliStampString : String {
        let timeInterval: TimeInterval = self.timeIntervalSince1970
        let millisecond = CLongLong(round(timeInterval*1000))
        return "\(millisecond)"
    }
    
    func daysBetweenDate(toDate: Date) -> Int {
        let components = Calendar.current.dateComponents([.day], from: self, to: toDate)
        return components.day ?? 0
    }
    
    func currentimeyyyyMMdd() -> String {
        let dateFormatter = DateFormatter() // 创建一个日期格式器
        dateFormatter.locale = Locale.init(identifier: "zh_CN")
        dateFormatter.dateFormat = "yyyy.MM.dd"
        
        let endString = dateFormatter.string(from: Date())
        return endString
    }
    
    func currentimeyyyyMMddHHmmss() -> String {
        let dateFormatter = DateFormatter() // 创建一个日期格式器
        dateFormatter.locale = Locale.init(identifier: "zh_CN")
        dateFormatter.dateFormat = "yyyy.MM.dd HH:mm:ss"
        
        let endString = dateFormatter.string(from: Date())
        return endString
    }
    
    func currentimeyyyyMMddHHmmssSSS() -> String {
        let dateFormatter = DateFormatter() // 创建一个日期格式器
        dateFormatter.locale = Locale.init(identifier: "zh_CN")
        dateFormatter.dateFormat = "yyyy.MM.dd HH:mm:ss SSS"
        
        let endString = dateFormatter.string(from: Date())
        return endString
    }
    
    func distancesSecondFrom(startTime:String) -> Int {
        let dateFormatter = DateFormatter() // 创建一个日期格式器
        dateFormatter.locale = Locale.init(identifier: "zh_CN")
        dateFormatter.dateFormat = "yyyy.MM.dd HH:mm:ss"
        
        let startD = dateFormatter.date(from: startTime) ?? Date.init(timeIntervalSince1970: TimeInterval.init(0))
        
        let endString = dateFormatter.string(from: Date())
        let endD = dateFormatter.date(from: endString) ?? Date.init(timeIntervalSince1970: TimeInterval.init(0))
        
        let gregorian = Calendar(identifier: Calendar.Identifier.gregorian)
        let comps = (gregorian as NSCalendar).components(NSCalendar.Unit.second, from: startD, to: endD, options: .wrapComponents)
        let distancesSecond = comps.second ?? 0
        return distancesSecond
    }
    
    func distancesDayFrom(startTime:String) -> Int {
        let dateFormatter = DateFormatter() // 创建一个日期格式器
        dateFormatter.locale = Locale.init(identifier: "zh_CN")
        dateFormatter.dateFormat = "yyyy.MM.dd HH:mm:ss"
        
        let startD = dateFormatter.date(from: startTime) ?? Date.init(timeIntervalSince1970: TimeInterval.init(0))
        let endString = dateFormatter.string(from: Date())
        let endD = dateFormatter.date(from: endString) ?? Date.init(timeIntervalSince1970: TimeInterval.init(0))
        
        let gregorian = Calendar(identifier: Calendar.Identifier.gregorian)
        let comps = (gregorian as NSCalendar).components(NSCalendar.Unit.day, from: startD, to: endD, options: .wrapComponents)
        let distancesDay = comps.day ?? 0
        return distancesDay
    }
    
    //时间转时间戳 2021/07/08 14:53:33 ->  16698557455874
    func timeToTimeStamp(time: String) -> Double {
        let dateFormatter = DateFormatter()
        dateFormatter.locale = Locale.init(identifier: "zh_CN")
        dateFormatter.dateFormat = "yyyy/MM/dd HH:mm:ss"
        let data = dateFormatter.date(from: time) ?? Date.init(timeIntervalSince1970: TimeInterval.init(0))
        let timeStamp = data.timeIntervalSince1970
        return timeStamp
    }
    
    //时间戳转换成几分钟前，几小时前，几天前
    func updateTimeToCurrennTime(timeStamp: Double) -> String {
        //获取当前的时间戳
        let currentTime = Date().timeIntervalSince1970
        //时间戳为毫秒级要 ／ 1000， 秒就不用除1000，参数带没带000
        let timeSta:TimeInterval = TimeInterval(timeStamp)
        //时间差
        let reduceTime : TimeInterval = currentTime - timeSta
        //时间差小于60秒
        if reduceTime < 60 {
            return "刚刚"
        }
        //时间差大于一分钟小于60分钟内
        let mins = Int(reduceTime / 60)
        if mins < 60 {
            return "\(mins)分钟前"
        }
        let hours = Int(reduceTime / 3600)
        if hours < 24 {
            return "\(hours)小时前"
        }
        let days = Int(reduceTime / 3600 / 24)
        if days < 30 {
            return "\(days)天前"
        }
        //不满足上述条件---或者是未来日期-----直接返回日期
        let date = NSDate(timeIntervalSince1970: timeSta)
        let dfmatter = DateFormatter()
        dfmatter.dateFormat = "yyyy年MM月dd日"
        dfmatter.locale = Locale.init(identifier: "zh_CN")
        return dfmatter.string(from: date as Date)
    }
}
